package cn.pping.basic.util;

import cn.pping.basic.jwt.JwtUtils;
import cn.pping.basic.jwt.Payload;
import cn.pping.basic.jwt.RsaUtils;
import cn.pping.user.domain.LoginData;
import cn.pping.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {

    public static Logininfo getLoginInfo(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            if (token != null) {
                PublicKey publicKey = RsaUtils.getPublicKey(RsaUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                //4.将jwtToken解析成想要的数据：LoginData
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                if (payload != null) {//登录成功，而且没有过期
                    //5.如获取登录信息对象
                    Logininfo logininfo = payload.getLoginData().getLogininfo();
                    return logininfo;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
